//
//  RunBuyOverViewController.m
//  rrqs
//
//  Created by 杜文杰 on 2018/8/31.
// 

#import "RunBuyOverViewController.h"
#import "RunBuyOverTableViewCell.h"
#import "MineTongJiViewController.h"
#import "PaoTuiDetailTwoViewController.h"

#define BGCOLOR "F1F1F1"
#define TOPVIEWHEIGHT 60
#define TITLELABEL_WIDTH 110

@interface RunBuyOverViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *mainTableView; 
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIView *footerView;
@property (nonatomic, strong) UILabel *contentLabel;
@property(nonatomic,assign) int page;// 页数
@property (nonatomic, assign) BOOL isGetMore;// 获取更多标识
@property (nonatomic, strong) NSMutableArray *datamuArray;
@end

@implementation RunBuyOverViewController
// 界面数据源
- (void)queryData {
    AFHTTPSessionManager *mgr=[AFHTTPSessionManager manager];
    NSDictionary* arg = @{@"ince":@"get_overorder_list",@"page":@"1",@"emp_id":[UserModel sharedInstanced].userId};
    NSString *netAddStr = @"http://xmpc.zadtek.com/App/Express/index";
    [mgr POST:netAddStr parameters:arg progress:nil success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject) {
        [WMHelper outPutJsonString:responseObject];
        self.contentLabel.text = [NSString stringWithFormat:@"%@单",responseObject[@"count"]];
        self.page++;
        if (self.isGetMore) {
            [self.datamuArray addObjectsFromArray:[responseObject objectForKey:@"data"]];
        }else {
            if (self.datamuArray.count > 0) {
                [self.datamuArray removeAllObjects];
            }
            [self.datamuArray addObjectsFromArray:[responseObject objectForKey:@"data"]];
        }
        [self.mainTableView reloadData];
        [self.mainTableView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask *_Nonnull operation, NSError * _Nonnull error) {
        [self alertHUD:@"网络异常"];
        [self.mainTableView.mj_header endRefreshing];
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"已完成的跑腿项目";
    self.page = 0;
    self.isGetMore = NO;
    [self setUpTopView];
    [self.view addSubview:self.mainTableView];
    [self queryData];
}

- (void)setUpTopView
{
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, TOPVIEWHEIGHT / HEIGHT_6S_SCALE)];
    topView.backgroundColor = getColor(whiteColor);
    [self.view addSubview:topView];
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake((SCREEN_WIDTH - TITLELABEL_WIDTH / WIDTH_6S_SCALE - 20 / HEIGHT_6S_SCALE) * 0.5, (TOPVIEWHEIGHT - 20) * 0.5, TITLELABEL_WIDTH / WIDTH_6S_SCALE, 20 / HEIGHT_6S_SCALE)];
    titleLabel.backgroundColor = getColor(whiteColor);
    titleLabel.text = @"累计跑腿单量:";
    [self.view addSubview:titleLabel];
    
    UILabel *contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(titleLabel.frame), (TOPVIEWHEIGHT - 20 / HEIGHT_6S_SCALE) * 0.5, TITLELABEL_WIDTH / WIDTH_6S_SCALE, 20 / HEIGHT_6S_SCALE)];
    contentLabel.textColor = getColor(@"F7AB3B");
    contentLabel.font = DEF_FontSize_15;
//    contentLabel.text = @"2单";
    self.contentLabel = contentLabel;
    [self.view addSubview:contentLabel];
}
#pragma mark -UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.datamuArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RunBuyOverTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([RunBuyOverTableViewCell class])];
    cell.dataDic = self.datamuArray[indexPath.section];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150 / HEIGHT_6S_SCALE;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10 / HEIGHT_6S_SCALE;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10 / HEIGHT_6S_SCALE)];
    view.backgroundColor = getColor(@BGCOLOR);
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
    
    return view;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PaoTuiDetailTwoViewController *VC = [[PaoTuiDetailTwoViewController alloc]init];
    VC.dataDic = self.datamuArray[indexPath.section];
    [self.navigationController pushViewController:VC animated:YES];
}
#pragma mark - init
- (UITableView *)mainTableView
{
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, TOPVIEWHEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - SafeAreaBottomHeight - SafeAreaTopHeight - TOPVIEWHEIGHT) style:UITableViewStyleGrouped];
        _mainTableView.backgroundColor = getColor(@BGCOLOR);
        _mainTableView.tableHeaderView = self.headerView;
        _mainTableView.tableFooterView = self.footerView;
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.showsVerticalScrollIndicator = NO;
        _mainTableView.showsHorizontalScrollIndicator = NO;
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        // 动态行高
        _mainTableView.rowHeight = UITableViewAutomaticDimension;
        // 预估行高
        _mainTableView.estimatedRowHeight = 80;
        [_mainTableView registerClass:[RunBuyOverTableViewCell class] forCellReuseIdentifier:NSStringFromClass([RunBuyOverTableViewCell class])];
        //刷新、加载
        _mainTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.isGetMore = NO;
            self.page = 0;
            [self queryData];
        }];
        
        _mainTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            self.isGetMore = YES;
            [self queryData];
        }];
    }
    return _mainTableView;
}
- (UIView *)headerView
{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
        _headerView.backgroundColor = [UIColor grayColor];
    }
    return _headerView;
}
- (UIView *)footerView
{
    if (!_footerView) {
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
    }
    return _footerView;
}
- (NSMutableArray *)datamuArray
{
    if (!_datamuArray) {
        _datamuArray = @[].mutableCopy;
    }
    return _datamuArray;
}
@end
